<?php
class Login extends Controller{
    public $data;
    function __construct(){
        parent::__construct();
        Session::init();
        $this->bar = new Bar();
        $this->model("model_website");
    }

    public function index(){
        if (Session::get("uid")){
            header('location:' . ROOT_URL);
        }else{
            if(isset($_POST['username'])){
                $username = $_POST['username'];
                $password = md5($_POST['password']);
                $data = $this->model_website->getUserByUsername($username);
                //variable
                $uid = "";
                $pass = "";
                foreach($data as $value){
                    $pass = $value['password'];
                    $uid = $value['uid'];
                }
                if($password == $pass){
                    Session::set("uid", $uid);
                    Session::commit();
                    if(Session::get("url")){
                        header('location:' . ROOT_URL.Session::get("url"));
                    }else{
                        header('location:' . ROOT_URL);
                    }
                }
            }
            //Bar content
            $bar_content = $this->bar->getBarLogin();
            $this->data['bar_content'] = $bar_content;
            $this->data['view'] = "login";
            /*$this->data['type'] = "user";*/
            $this->data['title'] = "Đăng nhập vào buzzy";
            $this->data['data'] = "";
            $this->view->render("layout/body", $this->data);
        }
    }

    public function doLogin(){
        if(isset($_POST['username'])){
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $data = $this->model_website->getUserByUsername($username);
            //variable
            $pass = "";
            foreach($data as $value){
                $pass = $value['password'];
            }
            if($password == $pass){
                echo json_encode(array("status"=>"ok"));
            }else{
                echo json_encode(array("status"=>"error"));
            }

        }else{
            echo json_encode(array("status"=>"error"));
        }
    }

    public function getForgetPassword(){
        if(isset($_POST['email'])){
            $email = $_POST['email'];
            $data = $this->model_website->getUserByEmail($email);
            if($data){
                $password = $this->libs->randomChars(8);
                $this->model_website->updateUserPassword($data[0]['uid'], md5($password));
                $content = "Mật khẩu mới ".$password;
                $this->mailer->sendMail("tuan.to@buzzmetrics.vn", $email, "Buzzmetrics - Khôi phục mật khẩu", htmlentities($content));
                echo json_encode(array("status"=>"ok"));
            }else{
                echo json_encode(array("status"=>"not_found"));
            }
        }else{
            echo json_encode(array("status"=>"error"));
        }
    }
}
?>